<?php
session_start();
date_default_timezone_set('PRC'); 
include "./public/common/config.php";

//如果未登录，用户名设为未登录，用户id设为0
$username = isset($_SESSION['home_username'])?$_SESSION['home_username']:'未登录';
$user_id = isset($_SESSION['home_userid'])?$_SESSION['home_userid']:0;
//$id = $_SESSION['home_userid'];

$sqlShelf = '';
if($user_id){
	$sqlUser = "select * from user where id = {$user_id}";
	$resUser = mysql_query($sqlUser);
	$rowUser = mysql_fetch_assoc($resUser);
	
	$sqlShelf = ' or weibo.user_id=' . $user_id;
}


$sqlWeibo = "select weibo.*,user.nick,user.img from weibo left join user on(weibo.user_id = user.id) where weibo.shelf=0" . $sqlShelf . " order by create_time desc";//echo $sqlWeibo;exit;
$resWeibo = mysql_query($sqlWeibo);


if($_POST){
	$des = $_POST['des'];
	$shelf = $_POST['shelf'];
	$user_id = $id;
	$create_time = time();
	
	$sql = "insert into weibo(des,shelf,user_id,create_time) values('{$des}','{$shelf}','{$user_id}','{$create_time}')";
	if(mysql_query($sql)){
		echo '<script>location="index.php"</script>';
	}
}
?>
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
	<meta charset="UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta name="renderer" content="webkit">
	<title>简易微博 主页</title>
	<!--[if lt IE 9]>
      <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	<script type="text/javascript" src="js/jquery-1.11.3.min.js" ></script>
	<script type="text/javascript" src="js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="css/bootstrap.min.css" />
	<link rel="stylesheet" type="text/css" href="css/font-awesome/css/font-awesome.min.css"/>
	<link rel="stylesheet" href="css/set.css" />
	<script type="text/javascript">
		function newmessage(){

			$.post("message.php",
		    {
		        name:"菜鸟教程",
		        url:"http://www.runoob.com"
		    },
		        function(data,status){
//		        alert("数据: \n" + data.name + "\n状态: " + status);
		    },"json");
		    
		    $.post("comment/manage.php",
		    {
		        name:"菜鸟教程",
		        url:"http://www.runoob.com"
		    },
		        function(data,status){
		        	$(".commentNumShelf").text(data.commentNumShelf);
		    },"json");
		}
		$(function(){
			setInterval("newmessage()",2000);
		});
	</script>
</head>
<body>
<!--<a href="detail.php?page=0">1</a><a href="detail.php?page=1">2</a><a href="detail.php?page=2">下一页</a>-->
<nav class="navbar navbar-default navbar-fixed-top ks_nav" role="navigation">
	<div class="container">
		<div class="navbar-header">
			<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#ks_nav">
				<span class="icon-bar"></span>
	        	<span class="icon-bar"></span>
	        	<span class="icon-bar"></span>
      		</button>
			<a class="navbar-brand" href="#">简易微博 - Beta v1.0.0</a>
		</div>

		<div class="collapse navbar-collapse" id="ks_nav">
			<form class="navbar-form navbar-left" role="search">
				<div class="form-group">
					<input type="text" class="form-control" placeholder="找人...">
				</div>
				<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-zoom-in"></span></button>
			</form>
			<ul class="nav navbar-nav navbar-right">
				<li><a href="userset.php">注册</a></li>
				<li><a href="#" data-toggle="modal" data-target="#useron">登陆</a></li>
				
				<li class="nav_user"><a href="userall.php"><span class="glyphicon glyphicon-user"></span><?php echo $username?></a></li>
				<li><a href="#">登出</a></li>
				<li><a href="#">关于作者</a></li>
				<li><a href="#">返回顶部</a></li>
			</ul>
		</div>
	</div>
</nav>

<div class="container">
	<div class="row">
		
		<div class="col-md-3 col-md-offset-1 col-md-push-7">
			
			<?php
			if($user_id){
			?>
			<!-- 登陆后 -->
			<div class="cont top_right">
				<div class="row">
					<div class="col-md-6 col-md-offset-3">
						<a href="userall.html"><img src="img/<?php echo $rowUser['img']?>" width="100%" class="img-circle hidden-xs"></a>
					</div>
					<div class="col-md-12">
						<h5 class="text-center"><a href="userall.html"><?php echo $rowUser['nick']?></a></h5>
					</div>
				</div>
				<div class="row">
					<div class="col-md-12">
						<a class="btn btn-info btn-block" href="chat.php">私信 <span class="badge">42</span></a>
					</div>
					<div class="col-md-12">
					<?php
					$sqlCommentShelf = "select comment.* from comment left join weibo on(comment.weibo_id = weibo.id) where weibo.user_id = {$user_id}";
					$resCommentShelf = mysql_query($sqlCommentShelf);
					$commentNumShelf = mysql_num_rows($resCommentShelf);	
					?>
						<a class="btn btn-info btn-block" href="#">评论 <span class="badge commentNumShelf"><?php echo $commentNumShelf?></span></a>
					</div>
				</div>
			</div>
			<?php	
			}else{
			?>
			<!-- 登陆前 -->
			<div class="cont top_right user_on">
				<h4 class="text-center">登陆 / 注册</h4>
				<div class="row">
					<div class="col-md-12">
						<a class="btn btn-info btn-block" href="#" data-toggle="modal" data-target="#useron">登&nbsp;&nbsp;陆</a>
					</div>
					<div class="col-md-12">
						<a class="btn btn-info btn-block" href="userset.html">注&nbsp;&nbsp;册</a>
					</div>
				</div>
			</div>
			<?php	
			}
			?>
			
			
			
			
			<!-- /.top_right -->
			
			<ul class="list-group cont_right hidden-md hidden-lg hidden-sm">
				<li class="list-group-item active">最新加入</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
				<li class="list-group-item"><span class="badge"><a href="#">撩TA</a></span>user name</li>
			</ul>
			<!-- /.cont_right -->
		</div>
		
		<div class="col-md-7 col-md-pull-3">
			<div class="cont top_left">
				<h4>分享每天好心情...</h4>
				<form action="index.php" method="post" enctype="multipart/form-data">
					<textarea class="form-control" rows="7" placeholder="说明文字" name="des"></textarea>
					
					<div class="row">
						<div class="col-md-3 col-md-offset-6">
							<select class="form-control" name="shelf">
								<option value="1">仅用户可见</option>
								<option value="0">所有人可见</option>
							</select>
						</div>
						<div class="col-md-3">
							<input type="submit" class="btn btn-primary btn-block" value="发布">
						</div>
					</div>
					
				</form>
			</div>
			<!-- /.top_left -->
			
		<?php
		while($rowWeibo = mysql_fetch_assoc($resWeibo)){
		?>
			<div class="cont cont_left">
				<div class="c_header">
					<img src="img/<?php echo $rowWeibo['img']?>" class="img-circle hidden-xs c_pho">
					<h4><?php echo $rowWeibo['nick']?><small>发布于：<?php echo date("Y-m-d H:m:s",$rowWeibo['create_time'])?></small></h4>
				</div>
				<div class="c_cont">
					<?php echo $rowWeibo['des']?>
				</div>
				<div class="c_footer">
					
					<div class="btn-group btn-group-justified">
						<div class="btn-group">
						<?php
						$sqlLike = "select * from likes where weibo_id = {$rowWeibo['id']}";
						$resLike = mysql_query($sqlLike);
						$likeNum = mysql_num_rows($resLike);
						
						if($user_id){
							$sqlMyLike = "select * from likes where weibo_id = {$rowWeibo['id']} and user_id = {$_SESSION['home_userid']}";
							$resMyLike = mysql_query($sqlMyLike);
							$MyLikeNum = mysql_num_rows($resMyLike);
						?>
							<a href="like/likes.php?id=<?php echo $rowWeibo['id']?>&like=<?php echo $MyLikeNum?>" class="btn btn-info">
								<i class="fa fa-heart<?php echo $MyLikeNum?'':'-o'?>" aria-hidden="true"></i>	
								<span class="badge"><?php echo $likeNum?></span>
							</a>
						<?php	
						}else{
						?>
							<a href="javascript:;" class="btn btn-info">
								<i class="fa fa-heart-o" aria-hidden="true"></i>
								<span class="badge"><?php echo $likeNum?></span>
							</a>
						<?php	
						}
						?>
						</div>
						<div class="btn-group">
						<?php
						$sqlComment = "select * from comment where weibo_id = {$rowWeibo['id']}";
						$resComment = mysql_query($sqlComment);
						$commentNum = mysql_num_rows($resComment);

						if($user_id){
						?>
							<a href="comment.php?weibo_id=<?php echo $rowWeibo['id']?>" class="btn btn-info">
								评论 <span class="badge"><?php echo $commentNum?></span>
							</a>
						<?php	
						}else{
						?>
							<a href="javascript:;" class="btn btn-info">
								评论 <span class="badge"><?php echo $commentNum?></span>
							</a>
						<?php	
						}
						?>
						</div>
						<div class="btn-group">
							<a href="#" class="btn btn-info">私信 / 修改</a>
						</div>
					</div>
					
				</div>
			</div>		
		<?php	
		}
		?>
			
			
		</div>

	</div>
</div>

<!-- 用户登陆弹框 -->
<div class="modal fade" id="useron" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="myModalLabel">用户登陆</h4>
			</div>
			<form action="check.php" method="post" role="form">
				<div class="modal-body">
					<div class="form-group">
						<label>账号：</label>
						<input type="text" class="form-control" name="username" placeholder="xxx">
					</div>
					<div class="form-group">
						<label>密码：</label>
						<input type="password" class="form-control" name="password" placeholder="xxx">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<input type="submit" class="btn btn-primary" value="登陆">
				</div>
			</form>
		</div>
	</div>
</div>

</body>
</html>
